import Cat2DocList from './list'
import Cat2DocTools from './tools'
import Cat2DocContent from './content';
import Cat2DocLoading from './loading'
import Cat2DocFile from './utils/file/index'

/**
 * 文档管理类
 */
class Cat2Doc {
  static version = '1.0.0';
  // 文档根区域
  #c2dDiv = null;
  // 文档区域
  #content = null;
  // 左侧区域
  #leftDiv = null;
  // 文档目录
  #list = null;
  // 右侧区域
  #rightDiv = null;
  // 浮动工具
  #tools = null;
  // 加载组件
  #loading = null;
  // c2d文件操作类
  #file = null;

  constructor(parentElement, options) {
    if(parentElement) {
      this.init(parentElement, options);
    }
  }
  /** 初始化 */
  init(parentElement, options) {
    this.options = {...{
      edit: true
      }, ...options}
    this.#c2dDiv = createC2dDiv(parentElement);
    this.#leftDiv = createLeftDive(this.#c2dDiv);
    this.#content = new Cat2DocContent(this.#c2dDiv, options);
    this.#rightDiv = createRightDiv(this.#c2dDiv);
    this.#tools = new Cat2DocTools(this.#c2dDiv);
    this.#list = new Cat2DocList(this.#leftDiv);
    this.#file = new Cat2DocFile(this.#content);
    this.#loading = new Cat2DocLoading(this.#c2dDiv);
    // 监听触发锚点事件
    window.addEventListener('anchor', e=>{
      const ele = this.#content.getRowElement(e.detail.id);
      if(ele) {
        this.#tools.show();
        this.#tools.setPos(this.#content.left-this.#tools.width-10, this.#tools.top);
        this.#tools.moveTo(ele);
      }
    });
    this.#content.$el.addEventListener('row-mouse-enter', event=>{
      this.#tools.show();
      this.#tools.setPos(this.#content.left-this.#tools.width-10, this.#tools.top);
      this.#tools.moveTo(event.detail.target);
    });
    this.#content.$el.addEventListener('row-mouse-leave', ()=>{
    });
    this.#content.$el.addEventListener('row-mouse-move', ()=>{
    });
    this.#content.$el.addEventListener('added-nodes', ()=>{
      this.#tools.hide();
      this.#list.loadList(this.#content.$el);
    });
    this.#content.$el.addEventListener('removed-nodes', ()=>{
      this.#tools.hide();
      this.#list.loadList(this.#content.$el);
    });
    // 设置焦点在文档中
    this.#content.focus();
  }
  /** 释放 */
  destory() {
    this.#tools.destory();
    this.#list.destory();
    this.#content.destory();

    this.#file=null;
    this.#tools=null;
    this.#list=null;
    this.#content=null;
  }
  /** 导出文件 */
  export(fileName) {
    return this.#file.export(fileName, this.#content.$el);
  }
  /** 导入文件 */
  import(file) {
    this.#loading.show();
    return new Promise((resolve,reject)=> {
      try {
        this.#content.reset();
        this.#file.import(file).then(value=>{
          this.#content.loadContent(value);
          this.#loading.hide();
          resolve(value);
        }).catch(err=>{
          this.#loading.hide();
          reject(err)
        })
      } catch (e) {
        this.#loading.hide();
        reject(e)
      }
    });
  }
  /** 从url导入文件 */
  importFormUrl(url) {
    const self = this;
    return new Promise((resolve,reject)=> {
      try {
        let xhr = new XMLHttpRequest();
        xhr.open("get", url, true);
        xhr.responseType = "blob";
        xhr.onload = function (res) {
          if (res.target.status === 200) {
            let fileExtension = Cat2DocFile.getExtension(res.target.responseURL);
            if(!fileExtension) {
              throw new Error('Unknown file type!');
            }
            fileExtension = fileExtension.toUpperCase();
            self.#file.import(res.target.response, fileExtension).then(value=>{
              self.#content.loadContent(value);
              self.#loading.hide();
              resolve(value);
            });
          } else {
            reject(res.target.statusText);
          }
        };
        xhr.send();
      } catch (e) {
        this.#loading.hide();
        reject(e)
      }
    });
  }
}




/** 创建主div */
function createC2dDiv(parent) {
  let div = document.createElement('div');
  div.className = 'c2d';
  parent.appendChild(div);
  return div;
}

/** 创建左侧目录div */
function createLeftDive(parent) {
  let div = document.createElement('div');
  div.className = 'c2d-left';
  div.style.zIndex = '1';
  div.style.overflowX = 'hidden';
  div.style.overflowY = 'auto';
  parent.appendChild(div);
  return div;
}
/** 创建右侧div */
function createRightDiv(parent) {
  let div = document.createElement('div');
  div.className = 'c2d-right';
  div.style.zIndex = '1';
  div.style.overflowX = 'hidden';
  div.style.overflowY = 'auto';
  parent.appendChild(div);
  return div;
}

export default Cat2Doc;
